'use strict'

define(['jquery' , 'bootstrap', 'shiftform', 'validate', 'validate.en'] , function($) {
  return {
  init : function(params){
    var that = this;
    that.siteId = params.siteId
    that.requestSignData();
    that.initButton();
    
    that.doSigin();
  },
  
  initButton: function() {
    var that = this;
    $("#openQrcode").on("click", function(){
      console.log(that.childWindow);
      $.ajax({
          url: window.__CTX__.root + "/is-overdue",
          type: 'GET',
          dataType:'text',
          success: function(res) {
            if(res.indexOf('{"code":"0",') == 0) {
                if(!that.childWindow) {
                    that.childWindow = window.open("welcome?siteId=" + that.siteId, "签到二维码", 'width='+(window.screen.availWidth-10)+',height='+(window.screen.availHeight-30)+',top=0,left=0,resizable=yes,status=yes,menubar=no,scrollbars=yes');
                    setTimeout(function(){
                      that.childWindow.openQrcode();
                    }, 2000)
                } else {
                    that.childWindow.openQrcode();
                }
            } else {
                if(that.childWindow) {
                    that.childWindow.close();
                }
                window.location.href='http://signin.test.fit-start.co/login';
            }
          }
        });
    })
    
    $("#openAd").on("click", function(){
      that.childWindow = window.open("welcome?siteId=" + that.siteId, "签到二维码", 'width='+(window.screen.availWidth-10)+',height='+(window.screen.availHeight-30)+',top=0,left=0,resizable=yes,status=yes,menubar=no,scrollbars=yes');
    })
  },
  
  requestSignData: function() {
    var that = this;
    window.requestSignData = function() {
      var message = that.childWindow.showQrcodeText();
      if(!that.ajaxShow) {
        $.ajax({
          url: window.__CTX__.root + "/signin-record?siteId=" + that.siteId + '&message=' + message,
          type: 'GET',
          success: function(res) {
            var res = typeof res === 'String' ? JSON.parse(res) : res
            if(res.code == '0') {
              that.ajaxShow = true
              that.signinData = res.data;
              that.showSigninForm(res.data);
              that.callChildrenSigninResult(res.data);
              
            } else {
              that.ajaxShow = false
              console.log(res.message);
            }
          }
        });
      }
    }
  },
  
  showSigninForm: function(data) {
    var that = this;
    $("#signinForm").removeClass("hide");
    $(".x-breadcrumb-wrapper").removeClass("hide")
    $("#scheduleId").val(data.courseSchedule.id);
    $("#courseName").html(data.courseName);
    $("#studentName").html(data.signinRecord.studentName)
    $("#coachName").html(data.signinRecord.coachName)
    $("#time").html(data.courseSchedule.occurDate + " " + data.courseSchedule.occurHour)
    $("#area").html(data.courseSchedule.roomType == 0 ? "公共空间" : "私密空间")
    $("#couldEdit").html(data.couldEdit ? "是" : "否")
    
    if(!data.couldEdit) {
      $("#bust").addClass("hide");
      $("#waist").addClass("hide");
      $("#hips").addClass("hide");
    } else {
      $("input[name='bust']").val("");
      $("input[name='waist']").val("");
      $("input[name='hips']").val("");
      $("#bust").removeClass("hide");
      $("#waist").removeClass("hide");
      $("#hips").removeClass("hide");
    }
    
  },
  
  callChildrenSigninResult: function(data) {
    var that = this;
    that.childWindow.showSigninResult(data);
  },
  
  doSigin: function() {
    var that = this;
    $('form[data-validate="true"]').bt_validate()
    $("#signinForm").shiftform({
      beforeSubmit : function(el) {
        $("#signinForm").trigger("validate.ajax.submit");
        
        if(that.signinData.couldEdit) {
          
          var $bust = $("input[name='bust']").val().trim();
          var $waist = $("input[name='waist']").val().trim();
          var $hips = $("input[name='hips']").val().trim();
          
          if(!$bust || !$waist || !$hips || isNaN($bust) || isNaN($waist) || isNaN($hips)) {
            $("#signinForm").show_tooltip("请输入正确的三围数据", 'remove', '#F2DEDE', '#EBCCD1', '#B94A48');
            return false;
          }
          
        }
        return $.bt_validate.result[$.bt_validate.form_id]
      },
      success: function() {
        setTimeout(function(){
          $("#signinForm").addClass("hide");
          $(".x-breadcrumb-wrapper").addClass("hide")
          $(".shiftform-backdrop").remove();
          $("button[type='submit']").attr("disabled", false);
          $("#scheduleId").val('');
          that.ajaxShow= false;
          that.childWindow.rebuild();
        }, 1000)
      },
      failure: function() {
        setTimeout(function(){
          $("#signinForm").addClass("hide");
          $(".x-breadcrumb-wrapper").addClass("hide")
          $(".shiftform-backdrop").remove();
          $("button[type='submit']").attr("disabled", false);
          $("#scheduleId").val('');
          that.ajaxShow= false;
          that.childWindow.rebuild();
        }, 1000)
      }
    })
  }
  }
})